﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Security.Cryptography;
using Friedlich.Web;

namespace Friedlich.Common
{
    public class Contact
    {
        private string _Name = string.Empty;
        public string Name {
            get {
                if (string.IsNullOrEmpty(_Name)) {
                    _Name = String.Format("{0} {1}", FirstName, LastName);
                }
                return _Name;
            }
            set {
                _Name = value;
            }
        }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        private List<string> _Emails;
        public List<string> Emails {
            get {
                if (_Emails == null) {
                    _Emails = new List<string>();
                }
                return _Emails;
            }
            set {
                _Emails = value;
            }
        }
        public string JobTitle { get; set; }
        public string CompanyName { get; set; }
        public List<Phone> PhoneNumbers { get; set; }
        public ContactList Contacts { get; set; }
        public List<Position> Positions { get; set; }
        public string LinkedInID { get; set; }

        public int Distance { get; set; }

        public XElement ToFoaf() { 

            XNamespace foafNS = Namespaces.FOAF;
            XNamespace resumeNS = Namespaces.ResumeRDF;
            XElement xContact = null;

            if (!string.IsNullOrEmpty(this.Name)) {
                xContact = new XElement(foafNS + "Person");
                
                XElement fullName = new XElement (foafNS + "name");
                fullName.SetValue(this.Name ?? string.Empty);
                xContact.Add(fullName);

                if (!string.IsNullOrEmpty(this.FirstName))
                {
                    XElement firstName = new XElement(foafNS + "firstName");
                    firstName.SetValue(this.FirstName);
                    xContact.Add(firstName);

                }

                if (!string.IsNullOrEmpty(this.LastName))
                {
                    XElement lastName = new XElement(foafNS + "surname");
                    lastName.SetValue(this.LastName);
                    xContact.Add(lastName);
                }

                foreach (string email in Emails)
                {
                    if (!string.IsNullOrEmpty(this.Email))
                    {
                        XElement mboxHash = new XElement(foafNS + "mbox_sha1sum");
                        mboxHash.SetValue(getSha1Sum(email));
                        xContact.Add(mboxHash);
                    }
                }

                if (Positions != null && Positions.Count > 0) {
                    XElement cvElem = new XElement(resumeNS + "CV");
                    foreach (Position position in Positions) {
                        XElement workHistory = new XElement(resumeNS + "WorkHistory");
                        
                        XElement jobTitle = new XElement(resumeNS + "jobTitle");
                        jobTitle.SetValue(position.Title);
                        workHistory.Add(jobTitle);
                        
                        XElement startDate = new XElement(resumeNS + "startDate");
                        startDate.SetValue(position.StartDate);
                        workHistory.Add(startDate);

                        if (position.EndDate != DateTime.MinValue) {
                            XElement endDate = new XElement(resumeNS + "endDate");
                            endDate.SetValue(position.EndDate);
                            workHistory.Add(endDate);
                        }

                        if (position.Company != null) {
                            XElement employedIn = new XElement(resumeNS + "employedIn");
                            XElement company = new XElement(resumeNS + "Company");
                            company.SetValue(position.Company.Name);
                            employedIn.Add(company);
                            workHistory.Add(employedIn);
                        }
                        cvElem.Add(workHistory);
                    }
                    xContact.Add(cvElem);
                }

                
            }
                                    
            return xContact;
        }

        private string getSha1Sum(string email) {
            StringBuilder sb = new StringBuilder();
            SHA1 md = new SHA1CryptoServiceProvider();
            if (!string.IsNullOrEmpty(email)) {
                byte[] digest = md.ComputeHash(Encoding.Default.GetBytes(email));
                foreach (byte i in digest)
                {
                    sb.Append(i.ToString("x2"));
                }
            }            
            return sb.ToString();
        }
    }
}
